/*
 * Copyright (C) 2010 Zaizi Limited.
 *
 * Alfresco FISE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco FISE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco FISE. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Content related component.
 * 
 * @namespace Alfresco
 * @class Alfresco.EntityRelated
 */
(function() {
	/**
	 * YUI Library aliases
	 */
	var Dom = YAHOO.util.Dom;

	/**
	 * Alfresco Slingshot aliases
	 */
	var $html = Alfresco.util.encodeHTML;

	/**
	 * EntityRelated constructor.
	 * 
	 * @param {String}
	 *            htmlId The HTML id of the parent element
	 * @return {Alfresco.EntityRelated} The new EntityRelated instance
	 * @constructor
	 */
	Alfresco.EntityRelated = function(htmlId) {
		Alfresco.EntityRelated.superclass.constructor.call(this, "Alfresco.EntityRelated", htmlId);

		return this;
	};

	YAHOO.extend(Alfresco.EntityRelated, Alfresco.component.Base, {	    
		/**
		 * Fired by YUI when parent element is available for scripting.
		 * Component initialisation, including instantiation of YUI widgets and
		 * event listener binding.
		 * 
		 * @method onReady
		 */
		onReady : function EDS_onReady() {
			this.loadingMessageShowing = false;
			this.loadingMessage = Alfresco.util.PopupManager.displayMessage({
				displayTime: 0,
				text: '<span class="wait">' + $html(this.msg("entity-related.loading")) + '</span>',
				noEscape: true
			});

			if (YAHOO.env.ua.ie > 0)
			{
				this.loadingMessageShowing = true;
			}
			else
			{
				this.loadingMessage.showEvent.subscribe(function() {
					this.loadingMessageShowing = true;
				}, this, true);
			}
		                		  
			// Request for related contents
			Alfresco.util.Ajax.request({
				url : Alfresco.constants.PROXY_URI +
						"zaizi/fiseiks/related?site=" + this.options.siteId + "&entity=" + this.options.entity,
				successCallback : {
					fn : this.onRelatedLoaded,
					scope : this
				},
				failureMessage : {
					fn : this.onRelatedFailure,
					scope : this
				}
			});
		},

		/**
		 * Success related content loaded
		 * 
		 * @param res {object} Response
		 * @method onRelatedLoaded
		 */
		onRelatedLoaded : function EntityRelated_onRelatedLoaded(res) {
			var contents = YAHOO.lang.JSON.parse(res.serverResponse.responseText);
			
			var o = '<ul>';
			for(var i=0; i<contents.length; i++) {
				o += this._createContent(contents[i]);
			}
			o += '</ul>';
			
			Dom.get(this.id + '-related').innerHTML = o;
			
			this._closeLoadingMessage();
		},

		/**
		 * Failure in related content loading
		 * 
		 * @param res {object} Response
		 * @method onRelatedFailure
		 */
		onRelatedFailure : function EntityRelated_onRelatedFailure(res) {
			Dom.setStyle(this.id + "-body", 'visibility', 'hidden');
			
			this._closeLoadingMessage();
		},
		
		/**
		 * Create a list entry for one content
		 * 
		 * @param content {object} Content information
		 * @method _createContent
		 */
		_createContent: function EntityRelated__createItem(content) {
			return '<li class="related-content">' +
				'<a href="document-details?nodeRef=' + content.nodeRef + '">' + content.title + '</a>' +
				'</li>';
		},
		
		_closeLoadingMessage: function EntityRelated__closeLoadingMessage() {
			var me = this;
			
			if (me.loadingMessage)
			{
				if (me.loadingMessageShowing)
				{
					me.loadingMessage.destroy();
					me.loadingMessage = null;
				}
				else
				{
					YAHOO.lang.later(100, me, me._closeLoadingMessage);
				}
			}
		}
	});
})();